<!--
 * @Author: your name
 * @Date: 2021-12-31 00:44:09
 * @LastEditTime: 2022-01-04 22:41:55
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /ac_interface/src/views/Admin.vue
-->
<template>

	<el-form ref="form" :model="form" label-width="90px"  style="margin:20px;width:60%;min-width:600px;">
        <h3>用户与虚拟机管理</h3>
        <el-form-item label="增加用户" >
			<td>
                <el-input v-model="form.addusername" placeholder="用户名"></el-input>
            </td>
            <td>
                <el-select v-model="form.adduserrole" placeholder="角色">
				    <el-option label="student" value="student"></el-option>
                    <el-option label="teacher" value="teacher"></el-option>
			    </el-select>
            </td>
            <td>
                <el-input v-model="form.addusernorpass" placeholder="普通密码"></el-input>
            </td>
            <td>
                <el-input v-model="form.adduserrootpass" placeholder="超级密码"></el-input>
            </td>
            <td>
                 <el-button type="primary"  @click="adduser" >增加</el-button>
            </td>
		</el-form-item>
        <el-form-item label="删除用户" >
			<td>
                <el-input v-model="form.delusername" placeholder="用户名"></el-input>
            </td>
            <td>
                 <el-button type="primary"  @click="deluser" :loading="logining">删除</el-button>
            </td>
		</el-form-item>
        <el-form-item label="增加虚拟机" >
			<td>
                <el-input v-model="form.vmname4" placeholder="name"></el-input>
            </td>
            <td>
                <el-input v-model="form.Ip" placeholder="Ip">
                </el-input>
            </td>
            <td>
                <el-input type="password" v-model="form.vmnormalpass" placeholder="vmnormalpass">
                </el-input>
            </td>
            <td>
                <el-input type="password" v-model="form.vmrootpass" placeholder="vmrootpass">
                </el-input>
            </td>
            <td>
                <el-select v-model="form.type" placeholder="type">
				    <el-option label="demo" value="demo"></el-option>
			    </el-select>
            </td>
            <td>
                 <el-button type="primary"  @click="addmachine" :loading="logining">增加</el-button>
            </td>
		</el-form-item>
        <el-form-item label="删除虚拟机" >
			<td>
                <el-input v-model="form.vmname5" placeholder="虚拟机名"></el-input>
            </td>
            <td>
                 <el-button type="primary"  @click="delmachine" :loading="logining">删除</el-button>
            </td>
		</el-form-item>
        <el-form-item label="查找虚拟机" >
			<td>
                <el-input v-model="form.vmname6" placeholder="虚拟机名"></el-input>
            </td>
            <td>
                 <el-button type="primary"  @click="findterminalbyname" :loading="logining">查找</el-button>
            </td>
		</el-form-item>		
        <h3>关联系统          <el-button @click="canVmlist">刷新</el-button>
            <el-select v-model="form.username" placeholder="选择用户" clearable>
				<el-option @click.native="canVmlist"
                    v-for='item in userList' 
                    :key="item" 
                    :label="item" 
                    :value="item">
                </el-option>
			</el-select>
        </h3>
		<el-form-item label="添加虚拟机" >
			<td>
                <el-input v-model="form.username" placeholder="用户名"></el-input>
            </td>
            <td>
                <el-select v-model="form.vmname1" placeholder="虚拟机名">
				    <el-option 
                        v-for='item in vmList' 
                        :key="item.name" 
                        :label="item.name" 
                        :value="item.name">
                    </el-option>
			    </el-select> 
            </td>
            <td>
                 <el-button type="primary"  @click="addVm" :loading="logining">添加</el-button>
            </td>
		</el-form-item>	
        <el-form-item label="删除虚拟机" >
			<td>
                <el-input v-model="form.username" placeholder="用户名"></el-input>
            </td>
            <td>
                <el-select v-model="form.vmname2" placeholder="虚拟机名">
				    <el-option 
                        v-for='item in vmList' 
                        :key="item.name" 
                        :label="item.name" 
                        :value="item.name">
                    </el-option>
			    </el-select>  
            </td>
            <td>
                 <el-button type="primary"  @click="delVm" :loading="logining">删除</el-button>
            </td>
		</el-form-item>
        <el-form-item label="列出虚拟机" >
            <td>
                <el-input v-model="form.username" placeholder="用户名">
                    </el-input>
            </td>
            <td>
                 <el-button type="primary"  @click="canVmlist" :loading="logining">列出</el-button>
            </td>
		</el-form-item>	
        <el-form-item label="可用虚拟机" >
            
        </el-form-item>	
    </el-form>
    
    <li v-for="item in vmList">{{item.name}}</li>
</template>


<script>
import { addvMatch, delMatch, canList,listMatch} from '../api/match.js';
import { addMachine,delMachine,findTerminalbyName} from '../api/vm.js';
import { listUser,addUser,delUser } from '../api/users.js'

export default {
	data() {
		return {
			form: {
                username:'',
                vmname1:'',
                vmname2:'',
                userList:[],
                vmList:[],
                vmname5:'',
                 name:'',
                 vmrole:''
			}
		}
	},
    mounted(){
        this.listuser();
    },
	methods: {
        deluser(){
            let ret=1;
            ret=delUser(this.form.delusername);
            if(ret==0){
                alert("删除成功");
            }
        },
        adduser(){
            let ret=1;
            ret=addUser(this.form.addusername,this.form.adduserrole,this.form.addusernorpass,this.form.addsuerrootpass);
            if(ret==0){
                alert("添加成功");
            }
        },
        listuser(){
            let ret=listUser();
            this.userList=ret.list;
        },
        addmachine(){
            let ret=1;
            ret=addMachine(this.form.vmname4,
            this.form.Ip,
            this.form.vmnormalpass,
            this.form.vmrootpass,this.form.type
            );
            if(ret==0){
                alert("添加成功");
            };
        },
        delmachine(){
            //alert(this.form.vmname5);
            let ret=1;
            ret=delMachine(this.form.vmname5);
            
            if(ret==0){
                alert("删除成功");
            }
            //alert(ret);
        },
        findterminalbyname(){
            //alert(this.form.vmname6);
            let ret=1;
            ret=findTerminalbyName(this.form.vmname6);
            if(ret==-1){
                //alert("没有这个虚拟机");
                return;
            }
            alert("虚拟机序号是"+ret);
        },
        addVm(){
            let ret={};
            ret=addvMatch(this.form.username,this.form.vmname1);
            if (ret.code==0){
                alert("添加成功")
            }
        },
        delVm(){
            let ret={};
            ret=delMatch(this.form.username,this.form.vmname2);
        },
        canVmlist(){
            let ret={};
            //alert(this.form.username3);
            ret=canList(this.form.username);
            this.vmList=ret.list;
            console.log(this.vmList);
            //alert(this.vmList) 
            this.$forceUpdate();
        }
	}
   
}

</script>

<style scoped>


</style>